﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WebBrowserTest.AnalyzeResults;
using HtmlAgilityPack;

namespace WebBrowserTest.Analyzers
{
    public class WordPerTagAnalyzer : AbstractAnalyzer
    {

        private string _keyword;
        //private Regex noLetterExpr = new Regex("^([^0-9A-Za-z]+)*$");
        private HtmlDocument _htmlDoc = new HtmlDocument();
        private WordPerTagAnalyzer()
        {
        }
        public WordPerTagAnalyzer(string keyword)
        {
            _keyword = keyword;
        }

     

        private Dictionary<string, int> GetDensityPerTag(string  url)
        {
            Dictionary<string, int> tagsCount = new Dictionary<string, int>();
            var hTagsAndTitle = GetHTagAndTitleDensity();
            var descTag = GetDescriptionTagDensity();
            foreach (KeyValuePair<string, int> hTags in hTagsAndTitle)
            {
                tagsCount.Add(hTags.Key,hTags.Value);
            }
            foreach (KeyValuePair<string, int> dTag in descTag)
            {
                tagsCount.Add(dTag.Key, dTag.Value);
            }
            tagsCount.Add("url",url.ToLower().Contains(_keyword.ToLower().Trim())?1:0);
            return tagsCount;
        }
        private Dictionary<string, int> GetHTagAndTitleDensity()
        {
            Dictionary<string, int> tagsCount = new Dictionary<string, int>();
            List<string> hTags = new List<string> { "h1", "h2", "h3", "title" };
            //Regex htmlTagFilter = new Regex("<[^<]+?>");
            foreach (string tag in hTags)
            {
                List<string> tagWords = new List<string>();

                var elements = _htmlDoc.DocumentNode.SelectNodes("//" + tag);
                if (elements == null || elements.Count == 0)
                {
                    tagsCount.Add(tag, 0);
                    continue;
                }
                foreach (var elem in elements)
                {
                    tagWords.Add(elem.InnerText);
                }
                
                tagsCount.Add(tag, tagWords.Where(o => o.ToLower().Contains(_keyword.ToLower())).Count());
            }
            return tagsCount;
        }
        private Dictionary<string, int> GetDescriptionTagDensity()
        {
            Dictionary<string, int> tagsCount = new Dictionary<string, int>();
            var descElement = _htmlDoc.DocumentNode.SelectSingleNode("//meta[@name=\"description\"]");
            if(descElement == null)
            {
                tagsCount.Add("description", 0);
                return tagsCount;
            }
            var descVal = descElement.Attributes["content"];
            if (descVal == null)
            {
                tagsCount.Add("description",0);
                return tagsCount;
            }

            tagsCount.Add("description", descVal.Value.Split(' ').Where(o => o.ToLower() == _keyword.ToLower()).Count());
            return tagsCount;
        }
        public override AbstractAnalyzeResult GetAnalyseResult(string html, string url)
        {
            _htmlDoc.LoadHtml(html);
            return new WordPerTagAnalyzeResult { Date = DateTime.Now, DomainName = url, TagDensity = GetDensityPerTag(url) };
        }


    }
    #region helper class

   
    #endregion
}
